package com.zenithmind.chat.util;

import com.zenithmind.chat.enums.*;

/**
 * 枚举转换工具类
 * 用于在重构过程中处理int值与枚举类型的转换
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
public class EnumConverter {

    /**
     * 将整数转换为好友状态枚举
     */
    public static FriendStatus toFriendStatus(Integer value) {
        if (value == null) return FriendStatus.NORMAL;
        return switch (value) {
            case 0 -> FriendStatus.DELETED;
            case 1 -> FriendStatus.NORMAL;
            case 2 -> FriendStatus.BLOCKED;
            default -> FriendStatus.NORMAL;
        };
    }

    /**
     * 将好友状态枚举转换为整数
     */
    public static Integer fromFriendStatus(FriendStatus status) {
        if (status == null) return 1;
        return switch (status) {
            case DELETED -> 0;
            case NORMAL -> 1;
            case BLOCKED -> 2;
        };
    }

    /**
     * 将整数转换为成员角色枚举
     */
    public static MemberRole toMemberRole(Integer value) {
        if (value == null) return MemberRole.MEMBER;
        return switch (value) {
            case 1 -> MemberRole.OWNER;
            case 2 -> MemberRole.ADMIN;
            case 3 -> MemberRole.MEMBER;
            case 4 -> MemberRole.GUEST;
            default -> MemberRole.MEMBER;
        };
    }

    /**
     * 将成员角色枚举转换为整数
     */
    public static Integer fromMemberRole(MemberRole role) {
        if (role == null) return 3;
        return switch (role) {
            case OWNER -> 1;
            case ADMIN -> 2;
            case MEMBER -> 3;
            case GUEST -> 4;
        };
    }

    /**
     * 将整数转换为成员状态枚举
     */
    public static MemberStatus toMemberStatus(Integer value) {
        if (value == null) return MemberStatus.NORMAL;
        return switch (value) {
            case 0 -> MemberStatus.EXITED;
            case 1 -> MemberStatus.NORMAL;
            case 2 -> MemberStatus.KICKED;
            case 3 -> MemberStatus.PENDING;
            case 4 -> MemberStatus.MUTED;
            case 5 -> MemberStatus.BANNED;
            default -> MemberStatus.NORMAL;
        };
    }

    /**
     * 将成员状态枚举转换为整数
     */
    public static Integer fromMemberStatus(MemberStatus status) {
        if (status == null) return 1;
        return switch (status) {
            case EXITED -> 0;
            case NORMAL -> 1;
            case KICKED -> 2;
            case PENDING -> 3;
            case MUTED -> 4;
            case BANNED -> 5;
        };
    }

    /**
     * 将整数转换为群组类型枚举
     */
    public static GroupType toGroupType(Integer value) {
        if (value == null) return GroupType.NORMAL;
        return switch (value) {
            case 1 -> GroupType.NORMAL;
            case 2 -> GroupType.COURSE;
            case 3 -> GroupType.PROJECT;
            case 4 -> GroupType.SYSTEM;
            case 5 -> GroupType.STUDY;
            case 6 -> GroupType.WORK;
            case 7 -> GroupType.INTEREST;
            default -> GroupType.NORMAL;
        };
    }

    /**
     * 将群组类型枚举转换为整数
     */
    public static Integer fromGroupType(GroupType type) {
        if (type == null) return 1;
        return switch (type) {
            case NORMAL -> 1;
            case COURSE -> 2;
            case PROJECT -> 3;
            case SYSTEM -> 4;
            case STUDY -> 5;
            case WORK -> 6;
            case INTEREST -> 7;
        };
    }

    /**
     * 将整数转换为群组状态枚举
     */
    public static GroupStatus toGroupStatus(Integer value) {
        if (value == null) return GroupStatus.ENABLED;
        return switch (value) {
            case 0 -> GroupStatus.DISABLED;
            case 1 -> GroupStatus.ENABLED;
            case 2 -> GroupStatus.REVIEWING;
            case 3 -> GroupStatus.DISSOLVED;
            case 4 -> GroupStatus.BANNED;
            default -> GroupStatus.ENABLED;
        };
    }

    /**
     * 将群组状态枚举转换为整数
     */
    public static Integer fromGroupStatus(GroupStatus status) {
        if (status == null) return 1;
        return switch (status) {
            case DISABLED -> 0;
            case ENABLED -> 1;
            case REVIEWING -> 2;
            case DISSOLVED -> 3;
            case BANNED -> 4;
        };
    }

    /**
     * 将整数转换为消息类型枚举
     */
    public static MessageType toMessageType(Integer value) {
        if (value == null) return MessageType.TEXT;
        return switch (value) {
            case 1 -> MessageType.TEXT;
            case 2 -> MessageType.IMAGE;
            case 3 -> MessageType.FILE;
            case 4 -> MessageType.VOICE;
            case 5 -> MessageType.VIDEO;
            case 6 -> MessageType.SYSTEM;
            case 7 -> MessageType.EMOJI;
            case 8 -> MessageType.LINK;
            case 9 -> MessageType.LOCATION;
            case 10 -> MessageType.CONTACT;
            default -> MessageType.TEXT;
        };
    }

    /**
     * 将消息类型枚举转换为整数
     */
    public static Integer fromMessageType(MessageType type) {
        if (type == null) return 1;
        return switch (type) {
            case TEXT -> 1;
            case IMAGE -> 2;
            case FILE -> 3;
            case VOICE -> 4;
            case VIDEO -> 5;
            case SYSTEM -> 6;
            case EMOJI -> 7;
            case LINK -> 8;
            case LOCATION -> 9;
            case CONTACT -> 10;
        };
    }

    /**
     * 将整数转换为消息状态枚举
     */
    public static MessageStatus toMessageStatus(Integer value) {
        if (value == null) return MessageStatus.NORMAL;
        return switch (value) {
            case 1 -> MessageStatus.NORMAL;
            case 2 -> MessageStatus.RECALLED;
            case 3 -> MessageStatus.DELETED;
            case 4 -> MessageStatus.FAILED;
            case 5 -> MessageStatus.SENDING;
            default -> MessageStatus.NORMAL;
        };
    }

    /**
     * 将消息状态枚举转换为整数
     */
    public static Integer fromMessageStatus(MessageStatus status) {
        if (status == null) return 1;
        return switch (status) {
            case NORMAL -> 1;
            case RECALLED -> 2;
            case DELETED -> 3;
            case FAILED -> 4;
            case SENDING -> 5;
        };
    }

    /**
     * 将字符串转换为添加来源枚举
     */
    public static AddSource toAddSource(String value) {
        if (value == null || value.trim().isEmpty()) return AddSource.OTHER;
        return switch (value.toLowerCase()) {
            case "friend_request", "好友申请" -> AddSource.FRIEND_REQUEST;
            case "phone_search", "手机号搜索" -> AddSource.PHONE_SEARCH;
            case "username_search", "用户名搜索" -> AddSource.USERNAME_SEARCH;
            case "qr_code", "二维码扫描" -> AddSource.QR_CODE;
            case "group_intro", "群组介绍" -> AddSource.GROUP_INTRO;
            case "system_recommend", "系统推荐" -> AddSource.SYSTEM_RECOMMEND;
            case "contact_import", "导入通讯录" -> AddSource.CONTACT_IMPORT;
            default -> AddSource.OTHER;
        };
    }

    /**
     * 将添加来源枚举转换为字符串
     */
    public static String fromAddSource(AddSource source) {
        if (source == null) return "other";
        return source.getCode();
    }
}
